package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.entity.Unit;
import com.atguigu.jxc.entity.vo.GoodsTypeList;

import java.util.HashMap;
import java.util.List;

/**
 * @author luojia
 * @create 2022-01-01 15:27
 */


@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private GoodsTypeDao goodsTypeDao;

    /**
     * 查询当前库存，(根据商品类别，商品编码或名称搜索)
     *
     * @param page
     * @param rows
     * @param codeOrName
     * @param goodsTypeId
     * @return
     */
    @Override
    public String listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        Integer initialRow = (page - 1) * rows;
        List<Goods> goodsList = goodsDao.listInventory(initialRow,rows,codeOrName,goodsTypeId);
        goodsList.forEach(goods -> {
            // 获取商品类别名称
            Integer typeId = goods.getGoodsTypeId();
            String goodsTypeName = goodsDao.getGoodsTypeName(typeId);
            // 获取商品 销售总量
            Integer goodsId = goods.getGoodsId();
            Integer num = goodsDao.getSaleTotal(goodsId);

            goods.setGoodsTypeName(goodsTypeName);
            goods.setSaleTotal(num);

        });

        int size = goodsList.size();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",size);
        map.put("rows",goodsList);
        Gson gson = new Gson();
        return gson.toJson(map);
    }

    /**
     * 查询当前库存，(根据商品类别，商品编码或名称搜索)
     * @param page
     * @param rows
     * @param goodsName
     * @param goodsTypeId
     * @return
     */
    @Override
    public List<Goods> queryAllGoods(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        Integer SizeStarter = (page-1)*rows;
        return this.goodsDao.queryAllGoods(rows,SizeStarter,goodsName,goodsTypeId);
    }


    @Override
    public List<Unit> queryUnit() {

        return this.goodsTypeDao.queryUnit();
    }

    @Override
    public void goodsTypeVoSave(String goodsTypeName, Integer pId) {
        this.goodsTypeDao.goodsTypeVoSave(goodsTypeName,pId);
    }

    @Override
    public void goodsTypeVoDelete(Integer  goodsTypeId) {
        this.goodsTypeDao.goodsTypeVoDelete(goodsTypeId);
    }

    @Override
    public void goodsAddUpdate(Goods goods, String goodsId) {
        if (goodsId!=null){
            this.goodsTypeDao.goodsUpdate(goods,goodsId);
        }else{
            goods.setState(0);
            goods.setInventoryQuantity(goods.getMinNum());
            this.goodsTypeDao.goodsAdd(goods);
        }
    }

    @Override
    public void goodsDelete(Integer goodsId) {

        this.goodsTypeDao.goodsDelete(goodsId);
    }

    @Override
    public List<Goods> queryNoInventoryQuantityGoods(Integer page, Integer rows, String nameOrCode) {
        Integer startNum = rows*(page-1);
        List<Goods> goods =  this.goodsTypeDao.queryNoInventoryQuantityGoods(rows,startNum,nameOrCode);
        return goods;
    }

    @Override
    public List<Goods> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Integer startNum = rows*(page-1);
        List<Goods> goods =  this.goodsTypeDao.getHasInventoryQuantity(rows,startNum,nameOrCode);
        return goods;
    }

    @Override
    public void updateGoods(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        this.goodsTypeDao.updateGoods(goodsId,inventoryQuantity,purchasingPrice);
    }

    @Override
    public void deleteStock(Integer goodsId) {
        this.goodsTypeDao.deleteStock(goodsId);
    }
}
